﻿<?php require_once('../Connections/STN.php'); ?>
<?php
if (!isset($_SESSION)) {
  session_start();
}
$MM_authorizedUsers = "";
$MM_donotCheckaccess = "true";

// *** Restrict Access To Page: Grant or deny access to this page
function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) { 
  // For security, start by assuming the visitor is NOT authorized. 
  $isValid = False; 

  // When a visitor has logged into this site, the Session variable MM_Username set equal to their username. 
  // Therefore, we know that a user is NOT logged in if that Session variable is blank. 
  if (!empty($UserName)) { 
    // Besides being logged in, you may restrict access to only certain users based on an ID established when they login. 
    // Parse the strings into arrays. 
    $arrUsers = Explode(",", $strUsers); 
    $arrGroups = Explode(",", $strGroups); 
    if (in_array($UserName, $arrUsers)) { 
      $isValid = true; 
    } 
    // Or, you may restrict access to only certain users based on their username. 
    if (in_array($UserGroup, $arrGroups)) { 
      $isValid = true; 
    } 
    if (($strUsers == "") && true) { 
      $isValid = true; 
    } 
  } 
  return $isValid; 
}

$MM_restrictGoTo = "login.php";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {   
  $MM_qsChar = "?";
  $MM_referrer = $_SERVER['PHP_SELF'];
  if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
  if (isset($_SERVER['QUERY_STRING']) && strlen($_SERVER['QUERY_STRING']) > 0) 
  $MM_referrer .= "?" . $_SERVER['QUERY_STRING'];
  $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
  header("Location: ". $MM_restrictGoTo); 
  exit;
}
?>
<?php include('componentes/enviafoto.class.php') ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}


//cadastro de fotos
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {


for($i=1;$i<6;$i++){
//verificação do campo
//o ["name"] e para pega o nome do arquivo

if($_FILES["arquivo$i"]["name"] != ""){
 
 
  $insertSQL = sprintf("INSERT INTO tb_fotos (FOT_ALB_CODIGO) VALUES (%s)",
                       GetSQLValueString($_POST['FOT_ALB_CODIGO'], "int"));

  mysql_select_db($database_STN, $STN);
  $Result1 = mysql_query($insertSQL, $STN) or die(mysql_error());
  
  
//pega o codigo da foto
	$query_pegafoto = "SELECT FOT_CODIGO from tb_fotos order by FOT_CODIGO desc LIMIT 1;";
	$RSpegafoto = mysql_query($query_pegafoto,$STN) or die(mysql_error());
	$totalLinhas_RSpegafoto = mysql_num_rows($RSpegafoto);
	$linha_RSpegafoto= mysql_fetch_assoc($RSpegafoto);
	
	
	$idfoto = $linha_RSpegafoto['FOT_CODIGO'];
  
//enviador de foto
  	$resultadofoto = enviafoto("arquivo$i","/imagem.jpg","fotosantiga",800,600,1,"$idfoto");

}

}
 

  $insertGoTo = "index.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));

}


mysql_select_db($database_STN, $STN);
$query_RSalbum = "SELECT alb.ALB_CODIGO, alb.ALB_TITULO FROM tb_albuns alb;";
$RSalbum = mysql_query($query_RSalbum, $STN) or die(mysql_error());
$row_RSalbum = mysql_fetch_assoc($RSalbum);
$totalRows_RSalbum = mysql_num_rows($RSalbum);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Documento sem título</title>
<script src="../SpryAssets/SpryValidationSelect.js" type="text/javascript"></script>
<link href="../SpryAssets/SpryValidationSelect.css" rel="stylesheet" type="text/css" />
</head>

<body>
<form action="<?php echo $editFormAction; ?>" method="post" enctype="multipart/form-data" name="form1" id="form1">
  <table align="center">
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">FOT_ALB_CODIGO:</td>
      <td><span id="spryselect1">
        <select name="FOT_ALB_CODIGO">
          <option></option>
          <?php 
do {  
?>
          <option value="<?php echo $row_RSalbum['ALB_CODIGO']?>" ><?php echo $row_RSalbum['ALB_TITULO']?></option>
          <?php
} while ($row_RSalbum = mysql_fetch_assoc($RSalbum));
?>
        </select>
      <span class="selectRequiredMsg">Selecione um item.</span></span></td>
    </tr>
    <tr>
      <td><label for="arquivo">SELECIONE AS FOTOS</label></td>
      <td>
      <input type="file" name="arquivo1" id="arquivo1" value="" /></td>
    </tr>
      <tr>
      <td><label for="arquivo2">&nbsp;</label></td>
      <td>
      <input type="file" name="arquivo2" id="arquivo2" value="" /></td>
    </tr>
      <tr>
      <td><label for="arquivo3">&nbsp;</label></td>
      <td>
      <input type="file" name="arquivo3" id="arquivo3" value="" /></td>
    </tr>
      <tr>
      <td><label for="arquivo4">&nbsp;</label></td>
      <td>
      <input type="file" name="arquivo4" id="arquivo4" value="" /></td>
    </tr>
      <tr>
      <td><label for="arquivo5">&nbsp;</label></td>
      <td>
      <input type="file" name="arquivo5" id="arquivo5" value="" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">&nbsp;</td>
      <td><input type="submit" value="Inserir registro" /></td>
    </tr>
  </table>
  <input type="hidden" name="MM_insert" value="form1" />
</form>
<p>&nbsp;</p>
<script type="text/javascript">
var spryselect1 = new Spry.Widget.ValidationSelect("spryselect1");
</script>
</body>
</html>
<?php
mysql_free_result($RSalbum);
?>
